package states
{
	import core.Assets;
	import core.Game;
	import core.MouseHelper;
	
	import flash.ui.Keyboard;
	import flash.ui.Mouse;
	import flash.utils.getTimer;
	
	import interfaces.IState;
	
	import managers.AlienManager;
	import managers.BulletManager;
	import managers.CollisionManager;
	import managers.ExplosionManager;
	
	import objects.Hero;
	import objects.Score;
	
	import starling.core.Starling;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.KeyboardEvent;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	
	public class Play extends Sprite implements IState
	{
		public var fire:Boolean = false;
		
		private var _game:Game;
		private var _hero:Hero;
		private var _score:Score;
		private var _bulletManager:BulletManager;
		private var _alienManager:AlienManager;
		private var _collisionManager:CollisionManager;
		private var _explosionManager:ExplosionManager;
		
		private var _touch:Touch;
		private var _isTouching:Boolean;
		
		private var _difficultyGapTime:Number = 5000;
		private var _lastDifficultyCheckTime:Number = 0;
		
		public function Play(game:Game)
		{
			trace("Play initialized.");
			super();
			
			this._game = game;
			this.addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function update(deltaSmall:Number):void
		{
			// simple movement using the native stage and mouse x and y
			//_hero.x += (Starling.current.nativeStage.mouseX - _hero.x) * 0.1;
			//_hero.y += (Starling.current.nativeStage.mouseY - _hero.y) * 0.1;
			
			if (_touch)
			{
				_hero.x += ((_touch.globalX - _hero.x) * _hero.easing) * _hero.moveSpeed * deltaSmall;
				_hero.y += ((_touch.globalY - _hero.y) * _hero.easing) * _hero.moveSpeed * deltaSmall;
				
				_hero.smokePS.emitterX = _hero.x;
				_hero.smokePS.emitterY = _hero.y + 35;
			}
			
			if (getTimer() - _lastDifficultyCheckTime > _difficultyGapTime)
			{
				_alienManager.spawnRate += .003;
				_lastDifficultyCheckTime = getTimer();
			}
			
			_hero.update(deltaSmall);
			_bulletManager.update(deltaSmall);
			_alienManager.update(deltaSmall);
			_collisionManager.update(deltaSmall);
		}
		
		public function destroy():void
		{
			MouseHelper.ToggleMouseVisibility();
			
			_hero.destroy();
			_bulletManager.destroy();
			_alienManager.destroy();
			_collisionManager.destroy();
			_score.destroy();
			
			_hero = null;
			_bulletManager = null;
			_alienManager = null;
			_collisionManager = null;
			
			stage.removeEventListener(TouchEvent.TOUCH, onTouched);
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private function init(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, init);
			
			MouseHelper.ToggleMouseVisibility();
			
			// WARNING: all display objects added to this class will also inherit touchable = false
			this.touchable = false;
			
			Game.SOUND_MANAGER.item(Assets.PLAY_BACKGROUND_MUSIC).play(int.MAX_VALUE);
			
			/**
			 * Create hero.
			 * */
			_hero = new Hero(this);
			this.addChild(_hero);
			_hero.x = stage.stageWidth * 0.5;
			_hero.y = stage.stageHeight - _hero.height * 0.5;
			
			_score = new Score();
			this.addChild(_score);
			_score.x = 450;
			
			_bulletManager = new BulletManager(this);
			_alienManager = new AlienManager(this);
			_collisionManager = new CollisionManager(this);
			_explosionManager = new ExplosionManager(this);
			
			_isTouching = false;
			
			/**
			 * Put the touch listener on the stage object otherwise touch events will only fire if you
			 * are over the object you added it to.
			 * */
			stage.addEventListener(TouchEvent.TOUCH, onTouched);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private function onKeyDown(e:KeyboardEvent):void
		{
			switch (e.keyCode)
			{
				case Keyboard.ESCAPE:
					MouseHelper.ToggleMouseVisibility();
					break;
				case Keyboard.SPACE:
					fire = true;
					break;
			}
		}
		
		private function onKeyUp(e:KeyboardEvent):void
		{
			switch (e.keyCode)
			{
				case Keyboard.ESCAPE:
					break;
				case Keyboard.SPACE:
					fire = false;
					break;
			}
		}
		
		private function onTouched(e:TouchEvent):void
		{
			_touch = e.getTouch(Starling.current.stage);
			if (_touch)
			{
				_isTouching = true;
			}
			else
			{
				_isTouching = false;
			}
		}

		public function get hero():Hero
		{
			return _hero;
		}

		public function get bulletManager():BulletManager
		{
			return _bulletManager;
		}

		public function set bulletManager(value:BulletManager):void
		{
			_bulletManager = value;
		}

		public function get alienManager():AlienManager
		{
			return _alienManager;
		}

		public function set alienManager(value:AlienManager):void
		{
			_alienManager = value;
		}

		public function get game():Game
		{
			return _game;
		}

		public function get explosionManager():ExplosionManager
		{
			return _explosionManager;
		}

		public function get score():Score
		{
			return _score;
		}

		public function set score(value:Score):void
		{
			_score = value;
		}


	}
}



































